﻿using System;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using ElectricDim;
using HYView;

namespace HYRElectric.WirePlace
{
	[Transaction(TransactionMode.Manual)]
	public class CmdWireConnect : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			if (Convert.ToDouble(commandData.Application.Application.VersionNumber) < 2015.0)
			{
				TaskDialog.Show("盈建科提示", "此功能目前只能在Revit 2015及以上版本使用。");
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			for (;;)
			{
				Transaction transaction = new Transaction(document, "WireConnect");
				transaction.Start();
				try
				{
					ChamferWireFilter chamferWireFilter = new ChamferWireFilter();
					Reference reference = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, chamferWireFilter, "请选择第一根需要连接的导线(只处理倒角导线)。");
					Reference reference2 = activeUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, chamferWireFilter, "请选择第二根需要连接的导线(只处理倒角导线)。");
					if (reference == null || reference2 == null)
					{
						return Autodesk.Revit.UI.Result.Failed;
					}
					RevitVersionFuncs.WireConnect(reference, reference2, document);
					transaction.Commit();
					continue;
				}
				catch (Exception)
				{
					transaction.RollBack();
				}
				break;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
